package com.xs.ahoi.hcxs.animation;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.GradientDrawable;
import android.widget.Scroller;

/**
 * 覆盖翻页
 */
public class CoverAnimation extends AnimationProvider {

    private Rect mSrcRect, mDestRect;
    private GradientDrawable mBackShadowDrawableLR;

    public CoverAnimation(Bitmap mCurrentBitmap, Bitmap mNextBitmap, int width, int height) {
        super(mCurrentBitmap, mNextBitmap, width, height);
        mSrcRect = new Rect(0, 0, mScreenWidth, mScreenHeight);
        mDestRect = new Rect(0, 0, mScreenWidth, mScreenHeight);

        // 设置阴影
        int[] mBackShadowColors = new int[]{0x66000000, 0x00000000};
        mBackShadowDrawableLR = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT, mBackShadowColors);
        mBackShadowDrawableLR.setGradientType(GradientDrawable.LINEAR_GRADIENT);
    }

    @Override
    public void drawMove(Canvas canvas) {
        if (getDirection().equals(AnimationProvider.Direction.next)) {
            // 计算右边边线的坐标
            int dis = (int) (mScreenWidth - myStartX + mTouch.x);
            dis = dis > mScreenWidth ? mScreenWidth : dis;

            // 计算bitmap截取的区域
            mSrcRect.left = mScreenWidth - dis;
            // 计算bitmap在canvas显示的区域
            mDestRect.right = dis;
            canvas.drawBitmap(mNextPageBitmap, 0, 0, null);
            canvas.drawBitmap(mCurPageBitmap, mSrcRect, mDestRect, null);
            addShadow(dis, canvas);
        } else {
            int dis = (int) (mTouch.x - myStartX);
            mSrcRect.left = mScreenWidth - dis;
            mDestRect.right = dis;
            canvas.drawBitmap(mCurPageBitmap, 0, 0, null);
            canvas.drawBitmap(mNextPageBitmap, mSrcRect, mDestRect, null);
            addShadow(dis, canvas);
        }
    }

    @Override
    public void drawStatic(Canvas canvas) {
        if (getCancel()) {
            canvas.drawBitmap(mCurPageBitmap, 0, 0, null);
        } else {
            canvas.drawBitmap(mNextPageBitmap, 0, 0, null);
        }
    }

    // 添加阴影
    private void addShadow(int left, Canvas canvas) {
        mBackShadowDrawableLR.setBounds(left, 0, left + 30, mScreenHeight);
        mBackShadowDrawableLR.draw(canvas);
    }

    @Override
    public void startAnimation(Scroller scroller) {

        int dx = 0;
        if (getDirection().equals(Direction.next)) {
            if (getCancel()) {
                int dis = (int) ((mScreenWidth - myStartX) + mTouch.x);
                if (dis > mScreenWidth) {
                    dis = mScreenWidth;
                }
                dx = mScreenWidth - dis;
            } else {
                dx = (int) -(mTouch.x + (mScreenWidth - myStartX));
            }
        } else {
            if (getCancel()) {
                dx = (int) -mTouch.x;
            } else {
                dx = (int) (mScreenWidth - (mTouch.x - myStartX));
            }
        }
        // 滑动速度保持一致
//        int duration = (200 * Math.abs(dx)) / mScreenWidth;
        scroller.startScroll((int) mTouch.x, 0, dx, 0, AnimationProvider.mySpeed);
    }

}
